//
//  HEAddNoticeViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/31.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAddNoticeViewController.h"
#import "HENoticePickerCell.h"
#import "HENoticeWeakCell.h"
#import "HENoticeDayCell.h"
#import "HENoticeTagCell.h"

@interface HEAddNoticeViewController ()<UITableViewDelegate, UITableViewDataSource, HENoticeWeakCellDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) UIButton *bottomView;
 

@end

@implementation HEAddNoticeViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"新增提醒";
    
    if ([DYTools isBlankString:_model.inst_id]) {
        _model.alarm_time = [NSDate getNowDay:NSDateFormaterModeTimeMinute];
        _model.notice_guardian = 1;
    }
     
    [self initUI];
    [self initCell];
    [self initTabBarView];
}
 
- (void)initUI {
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight- ZXNavBarHeight - ZXSafeAreaBottom - 49) style:UITableViewStyleGrouped];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    
   _tableView.separatorColor = CSplitLineColor;
   _tableView.scrollsToTop = YES;
   _tableView.tableFooterView = [[UIView alloc] init];
   
   self.tableView.backgroundColor = KWhiteColor;
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   [self.view addSubview:self.tableView];
}

- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];
 
    GDFiterCellModel *pickerModel = [GDFiterCellModel new];
    pickerModel.cellKlass = HENoticePickerCell.class;
  
    GDFiterCellModel *weakModel = [GDFiterCellModel new];
    weakModel.cellKlass = HENoticeWeakCell.class;
    
    GDFiterCellModel *dayModel = [GDFiterCellModel new];
    dayModel.cellKlass = HENoticeDayCell.class;
    
    GDFiterCellModel *tagModel = [GDFiterCellModel new];
    tagModel.cellKlass = HENoticeTagCell.class;
    
    [_sections addObject: pickerModel];
    [_sections addObject: weakModel];
    [_sections addObject: dayModel];
    [_sections addObject: tagModel];

   // 添加的时候注意顺序，暂未对顺序做处理
   [self.tableView mc_registerCellByFiterModels: _sections];
 
   [_tableView reloadData];
}

- (void)initTabBarView {
    UIButton *bottomView = [UIButton buttonWithType:UIButtonTypeCustom];
    _bottomView = bottomView;
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = CNavBgColor;
    _bottomView.titleLabel.font = DYBlodFont(18);
    [bottomView setTitleColor:KWhiteColor forState:UIControlStateNormal];
    [bottomView addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    
    [bottomView setTitle:@"保存" forState:UIControlStateNormal];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_offset(49);
        make.bottom.offset(-ZXSafeAreaBottom);
    }];
}

- (void)save {
    NSString *url = @"";
    
    if ([DYTools isBlankString:_model.alarm_week]) {
        [Dialog toastBottom:@"请选择提醒时间"];
        return;
    }
    if ([DYTools isBlankString:_model.alarm_title]) {
        [Dialog toastBottom:@"请选择提醒标签"];
        return;
    }
    
    
    if ([DYTools isBlankString:_model.inst_id]) {
        url = KServerSaveAlarms;
    }else {
        url = KServerUpdateAlarms;
    }
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:url parameters:_model success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        
        if (response.success) {
            [Dialog toastBottom:@"操作成功"];
            
            if(self.backBlock) {
                self.backBlock();
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }

    } failure:^(NSError * _Nonnull error) {
        
    }];
}
 

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return _sections.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   GDFiterCellModel *fiterModel = _sections[indexPath.row];
    MCTableBaseCell *cell = (MCTableBaseCell *)[_tableView mc_dequeueReusableCellWithClassType: fiterModel.cellKlass];
    cell.proxyVC = self;
    
    [cell configCellWithModel:_model indexPath:indexPath];
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
        [cell configCellWithModel:self.model indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
}
 
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark - HENoticeWeakCellDelegate
- (void)cellSelectWeak:(HENoticeWeakCell *)cell {
    [self.tableView reloadData];
}

#pragma mark - lazy
 
@end
 
